package org.ycx.doc.service.impl;

import org.ycx.common.annotation.BaseService;
import org.ycx.common.base.BaseServiceImpl;
import org.ycx.common.db.DataSourceEnum;
import org.ycx.common.db.DynamicDataSource;
import org.ycx.doc.dao.enums.GroupUsersRole;
import org.ycx.doc.dao.mapper.IDocGroupMapper;
import org.ycx.doc.dao.mapper.IDocModuleMapper;
import org.ycx.doc.dao.mapper.IDocProjectMapper;
import org.ycx.doc.dao.mapper.IDocProjectVersionMapper;
import org.ycx.doc.dao.model.*;
import org.ycx.doc.service.IDocProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* DocProjectService实现
* Created by yang on 2017-08-30 17:02:41.
*/
@Service
@Transactional
@BaseService
public class DocProjectServiceImpl extends BaseServiceImpl<IDocProjectMapper, DocProject, DocProjectExample> implements IDocProjectService {

    private static Logger _log = LoggerFactory.getLogger(DocProjectServiceImpl.class);

    @Autowired
    IDocProjectMapper mapper;

    @Autowired
    IDocGroupMapper groupMapper;

    @Autowired
    IDocProjectVersionMapper versionMapper;

    @Autowired
    IDocModuleMapper moduleMapper;

    @Override
    public List<DocProject> findProjectByUser(int uid, List<GroupUsersRole> roles) {
        DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
        List<Integer> rids = new ArrayList<Integer>();
        for (GroupUsersRole role:roles) {
            rids.add(role.getType());
        }
        return this.mapper.findProjectByUser(uid,rids);
    }

    @Override
    public int addProject(DocProject bean, Integer gid) {
        int res = this.mapper.insertSelective (bean);
        if(res > 0){
            return this.groupMapper.insertGroupProject(gid,bean.getAppId ());
        }else{
            return 0;
        }
    }

    @Override
    public List<DocGroup> findUserGroups(int uid) {
        return this.groupMapper.findDocGroupByUser (uid,GroupUsersRole.Master.getType ());
    }

    @Override
    public int publish(Integer appId, String ids) {
        DocProject project = this.mapper.selectByPrimaryKey (appId);
        int currentVersion = project.getAppProdVersion ();
        project.setAppProdVersion (currentVersion+1);
        project.setUpdateTs (new Date());
        int res = this.mapper.updateByPrimaryKey (project);
        DocProjectVersion dpv = new DocProjectVersion ();
        dpv.setAppId (project.getAppId ());
        dpv.setAppVersion (project.getAppProdVersion ());
        res += this.versionMapper.insert (dpv);
        String[] idsArray = ids.split ("-");
        for(int i=0;i<idsArray.length;i++){
            DocModule module = new DocModule ();
            module.setModuleId (Integer.parseInt (idsArray[i]));
            module.setUpdateTs (new Date ());
            module.setmVersion (currentVersion+1);
            res+=this.moduleMapper.updateByPrimaryKeySelective (module);
        }
        return project.getAppProdVersion ();
    }

    @Override
    public int findRoleByProjectAndUid(Integer appId, int uid) {
        return this.mapper.findRoleByAppAndUid(appId,uid);
    }
}